package com.ruoyi.web.controller.workSafetyStandardization.item;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.workSafetyStandardization.domain.ItemConfig;
import com.ruoyi.workSafetyStandardization.service.IItemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目配置Controller
 *
 * @author hcc
 * @date 2023-12-15
 */
@RestController
@RequestMapping("/secure/item")
public class ItemConfigController extends BaseController
{
    @Autowired
    private IItemConfigService itemConfigService;

    /**
     * 查询项目配置列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(@Param("majorId") String majorId, @Param("enable") Long enable,
                           @Param("pageSize") String pageSize,@Param("pageNum") String pageNum)
    {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        ItemConfig itemConfig =new ItemConfig();
        itemConfig.setMajorId(majorId);
        itemConfig.setEnable(enable);
        List<ItemConfig> list = itemConfigService.selectItemConfigList(itemConfig);
        return getDataTable(list);
//        return success(itemConfigService.queryItemByMajor(itemConfig));
    }
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:item:list')")
    @GetMapping("/query")
    public AjaxResult list(@Param("majorId") String majorId, @Param("enable") Long enable)
    {
        ItemConfig itemConfig =new ItemConfig();
        itemConfig.setMajorId(majorId);
        itemConfig.setEnable(enable);
        return success(itemConfigService.queryItemByMajor(itemConfig));
    }
//    public TableDataInfo list(ItemConfig itemConfig)
//    {
//        startPage();
//        List<ItemConfig> list = itemConfigService.selectItemConfigList(itemConfig);
//        return getDataTable(list);
//    }
    /**
     * 获取项目配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:item:query')")
    @GetMapping("/distribute")
    public AjaxResult query(@Param("itemId") String itemId)
    {
        return success(itemConfigService.selectItemConfigByItemId(itemId));
    }

    /**
     * 导出项目配置列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:item:export')")
    @Log(title = "项目配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItemConfig itemConfig)
    {
        List<ItemConfig> list = itemConfigService.selectItemConfigList(itemConfig);
        ExcelUtil<ItemConfig> util = new ExcelUtil<ItemConfig>(ItemConfig.class);
        util.exportExcel(response, list, "项目配置数据");
    }

    /**
     * 获取项目配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:item:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") String itemId)
    {
        return success(itemConfigService.selectItemConfigByItemId(itemId));
    }

    /**
     * 新增项目配置
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:item:add')")
    @Log(title = "项目配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItemConfig itemConfig)
    {
        return toAjax(itemConfigService.insertItemConfig(itemConfig));
    }

    /**
     * 修改项目配置
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:item:edit')")
    @Log(title = "项目配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItemConfig itemConfig)
    {
        return toAjax(itemConfigService.updateItemConfig(itemConfig));
    }

    /**
     * 删除项目配置
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:item:remove')")
    @Log(title = "项目配置", businessType = BusinessType.DELETE)
	@GetMapping("/delete")
    public AjaxResult remove(@Param("itemId") String itemId)
    {
        String[] itemIds={itemId};
        return toAjax(itemConfigService.deleteItemConfigByItemIds(itemIds));
    }
}
